package com.songshuai.sales.tool.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.songshuai.sales.tool.dto.request.sop.*;
import com.songshuai.sales.tool.dto.response.label.QueryLabelsPageResDto;
import com.songshuai.sales.tool.dto.response.sop.ArticleSopResDto;
import com.songshuai.sales.tool.dto.response.sop.ListResDto;
import com.songshuai.sales.tool.service.SopService;
import com.songshuai.sales.tool.util.PageUtil;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.enumerations.sales.SopStatusEnum;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryRes;
import com.songshuai.webapi.baseapi.utils.OssUploadUtil;
import com.songshuai.webapi.baseapi.utils.SnowFlakeUtil;
import com.songshuai.webapi.common.upload.BaseUploadFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Slf4j
@Api("SOP后台管理")
@RestController
@RequestMapping("/api/sop/")
public class SopController{

    @Autowired
    private SnowFlakeUtil snowFlakeUtil;
    @Autowired
    private OssUploadUtil uploadUtil;


    @Value("${file.basePath}")
    private String fileBasePath;

    @Autowired
    private SopService sopService;



    /**
     * sop列表
     */
    @ApiOperation(value = "sop列表")
    @RequestMapping(value = "showList", method = RequestMethod.POST)
    public QueryRes<List<ListResDto>> showList(@RequestBody BaseParmDto<ListReqDto> request) {

        try{

            Page<List<ListResDto>> page = PageHelper.startPage(request.getPageIndex(), request.getPageSize());
            List<ListResDto> listRecordResDtos = sopService.showList(request);
            return QueryPageRes.setQueryResData(listRecordResDtos, page.getTotal());

        } catch (Exception e) {
            log.error("api/sop/showList", e);
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }



    /**
     * 添加sop
     */
    @ApiOperation(value = "添加sop")
    @RequestMapping(value = "addSop", method = RequestMethod.POST)
    public QueryRes addSop(@RequestBody AddSopReqDto request) {

        try{

            if(null == request.getStatus() || request.getStatus() == SopStatusEnum.SopAddSopStatus.getCode()){
                sopService.addSop(request);
            }else{
                sopService.editSop(request);
            }

            return QueryRes.setResData(null);
        } catch (Exception e) {
            log.error("api/sop/addSop", e);
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }



    /**
     * 编辑sop
     */
    @ApiOperation(value = "编辑sop")
    @RequestMapping(value = "editSop", method = RequestMethod.POST)
    public QueryRes editSop(@RequestBody AddSopReqDto request) {

        try{
            sopService.editSop(request);
            return QueryRes.setResData(null);
        } catch (Exception e) {
            log.error("api/sop/editSop", e);
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }


    /**
     * sop删除
     */
    @ApiOperation(value = "sop删除")
    @RequestMapping(value = "deleteSop", method = RequestMethod.POST)
    public QueryRes deleteSop(@RequestBody DeleteSopReqDto request) {

        try{
            sopService.deleteSop(request);
            return QueryRes.setResData(null);
        } catch (Exception e) {
            log.error("api/sop/deleteSop", e);
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }



    /**
     * sop详情
     */
    @ApiOperation(value = "sop详情")
    @RequestMapping(value = "articleSop", method = RequestMethod.POST)
    public QueryRes articleSop(@RequestBody ArticleSopReqDto request) {

        try{
            ArticleSopResDto articleSopResDto = sopService.articleSop(request);
            return QueryRes.setResData(articleSopResDto);
        } catch (Exception e) {
            log.error("api/sop/articleSop", e);
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }





    /**
     * sop状态设置
     * 1：发布
     * 2：上下架
     * 3: 审核
     * 4: 置顶
     */
    @ApiOperation(value = "sop状态设置")
    @RequestMapping(value = "sopStatusSet", method = RequestMethod.POST)
    public QueryRes sopStatusSet(@RequestBody SopStatusSetReqDto request) {

        try{
            sopService.sopStatusSet(request);
            return QueryRes.setResData(null);
        } catch (Exception e) {
            log.error("api/sop/sopStatusSet", e);
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }

    /**
     * sop关联标签
     */
    @ApiOperation(value = "sop关联标签")
    @RequestMapping(value = "sopTagSet", method = RequestMethod.POST)
    public QueryRes sopTagSet(@RequestBody SopTagSetReqDto request) {

        try{
            sopService.sopTagSet(request);
            return QueryRes.setResData(null);
        } catch (Exception e) {
            log.error("api/sop/sopTagSet", e);
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }





    /**
     * 1：图片
     * 2：视频
     */
    @ApiOperation(value = "图片/视频上传")
    @RequestMapping(value = "uploadFile", method = RequestMethod.POST)
    public QueryRes uploadPicture(@RequestParam("file") MultipartFile file, int type) {

        try{
            BaseUploadFile baseUploadFile=new BaseUploadFile(snowFlakeUtil,fileBasePath,uploadUtil);
            Map<String, Object> map = null;

            if(type == 1){
                map = baseUploadFile.uploadFile(file,"image");
            }else{
                map = baseUploadFile.uploadFile(file,"video");
            }

            Integer code=Integer.parseInt(String.valueOf(map.get("code")));
            String msg=String.valueOf(map.get("msg"));
            map.remove("code");
            map.remove("msg");
            return QueryRes.setResData(map,code,msg);

        } catch (Exception ex) {
            log.error("api/sop/uploadFile", ex);
            throw new BusinessException(ResultStatus.ERROR_PIC_UPLOAD.getErrorMsg(), ResultStatus.FAILOfficial);
        }
    }


}
